<template>
	<view :style="theme" class="content">
		<image :src="imgHost+'/title_duihuan.png'" class="img"></image>
		<view class="ts">团购自助验券</view>
		<!-- #ifndef APP-NVUE -->
		<u-input v-model="keyword" placeholder="输入券码" clearable fontSize="30rpx"
			customStyle="height:88rpx;width: 100%;border-radius: 50rpx;box-sizing: border-box;flex: none;"
			inputAlign="center">
		<!-- #endif -->
			<!-- #ifdef APP-NVUE -->
			<u--input v-model="keyword" placeholder="输入券码" fontSize="30rpx"
				customStyle="height:88rpx;width: 100%;border-radius: 50rpx;flex: none;" inputAlign="center">
			<!-- #endif -->
				<u-icon color="var(--color)" size="48rpx" name="scan" slot="suffix" @click="saoma"></u-icon>
		<!-- #ifndef APP-NVUE -->
		</u-input>
		<!-- #endif -->
		<!-- #ifdef APP-NVUE -->
		</u--input>
		<!-- #endif -->
		<view class="btns" @click="getDetail">兑换</view>
		<view v-if="djsr && !keyword" class="tip">请输入正确的券码</view>
	</view>
</template>
<script>
	// #ifdef H5
	import wx from '@/common/jweixin.js';
	// #endif
	export default {
		data() {
			return {
				keyword: '',
				djsr: false
			}
		},
		methods: {
			jump(url, params, a) {
				this.$app.jump(url, params, a);
			},
			jump2(url, params, a) {
				this.$app.jump2(url, params, a);
			},
			saoma() {
				let that = this
				// #ifndef H5
				uni.scanCode({
					success(res) {
						if (res.result && res.rawData) {
							that.$app.ajax({
								data: {
									param: {
										memberId: that.userInfo.id,
										code: res.result,
										num: 1
									}
								},
								api: that.$api.affirmCheckGetMeals(),
							}).then((res) => {
								if (res.errorCode === '000000') {
									let orderId = res.record.orderId || ''
									uni.showToast({
										title: '验券成功',
										icon: 'success'
									})
									setTimeout(() => {
										uni.navigateTo({
											url: `/pages/order/code/code?orderId=${orderId}`
										})
										// uni.navigateBack({
										// 	delta: 1
										// })
									}, 500)
								}
							}).catch(() => {})
						}


					}
				})
				// #endif
				// #ifdef H5
				this.onScan()
				// #endif
			},
			// h5扫描二维码并解析
			onScan() {
				let that = this
				wx.scanQRCode({
					needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
					scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码，默认二者都有
					desc: 'scanQRCode desc',
					success: function(res) {
						var result = res.resultStr; // 当 needResult 为 1 时，扫码返回的结果
						var resultArr = result.split(','); // 扫描结果以逗号分割数组(一维码)
						var codeContent = resultArr[resultArr.length - 1]; // 获取数组最后一个元素，也就是最终的内容 
						if (result) {
							that.$app.ajax({
								data: {
									param: {
										memberId: that.userInfo.id,
										code: result,
										num: 1
									}
								},
								api: that.$api.affirmCheckGetMeals(),
							}).then((res) => {
								if (res.errorCode === '000000') {
									let orderId = res.record.orderId || ''
									uni.showToast({
										title: '验券成功',
										icon: 'success'
									})
									setTimeout(() => {
										uni.navigateTo({
											url: `/pages/order/code/code?orderId=${orderId}`
										})
										// uni.navigateBack({
										// 	delta: 1
										// })
									}, 500)
								}
							}).catch(() => {})
						}
					},
					fail: function(response) {
						console.log("调用wx.scanQRCode扫码失败");
					},
				});
			},
			getDetail() {
				if (!this.keyword) {
					// uni.showToast({
					// 	title: '请输入正确的券码',
					// 	icon: 'none'
					// })
					this.djsr = true
					return false
				}
				this.djsr = false
				this.$app.ajax({
					data: {
						param: {
							code: this.keyword,
							memberId: this.userInfo.id,
							num: 1
						}
					},
					api: this.$api.affirmCheckGetMeals(),
				}).then((res) => {
					if (res.errorCode === '000000') {
						let orderId = res.record.orderId || ''
						uni.showToast({
							title: '验券成功',
							icon: 'success'
						})
						uni.navigateTo({
							url: `/pages/order/code/code?orderId=${orderId}`
						})
						// setTimeout(() => {
						// 	uni.navigateBack({
						// 		delta: 1
						// 	})
						// }, 500)
					}
				}).catch(() => {})
			},
		}

	}
</script>
<style scoped lang="scss">
	.content {
		padding: 135rpx 30rpx 0;
		box-sizing: border-box;
		background: #ffffff;
		width: 690rpx;
		margin: 30rpx;
		height: calc(100vh - 60px);
		align-items: center;
		display: flex;
		flex-direction: column;
	}

	.img {
		width: 272rpx;
		height: 152rpx;
	}

	.ts {
		font-size: 36rpx;
		font-family: PingFangSC-Semibold, PingFang SC;
		font-weight: 600;
		color: #222222;
		line-height: 36rpx;
		margin: 44rpx auto 80rpx;
		text-align: center;
	}

	.btns {
		width: 100%;
		line-height: 88rpx;
		color: #fff;
		margin-top: 40rpx;
		text-align: center;
		background-color: $color;
		border-radius: 50rpx;
		font-size: 30rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: #fff;
	}

	.saoma {
		width: 48rpx;
		height: 48rpx;
	}

	.tip {
		font-size: 26rpx;
		margin-top: 40rpx;
		color: $red;
		line-height: 1;
		text-align: center;
	}
</style>